const app = getApp()
import store from '../../utils/store'
Page({
    data: {
        searchText: '',
        pageNum: 1,
        pageSize: 10,
        curSelectId: '',
        companyList: [],
        isNoData: false,
        searchCompanyList: [], // 用来展示搜索结果的数组
        onlineCpmpanyList: [], // 在线待机项目现场
        defaultCompanyList: [], // 不在线项目现场
        loadingCompanyList: [], // 在线装车中项目现场
    },
    handleSearchChange(e) {
        console.log(e)
        const searchText = e.detail.value
        this.setData({
            searchText
        })
    },
    handleSearch(e) {
        this.setData({
            loadingCompanyList: [],
            onlineCpmpanyList: [],
            defaultCompanyList: []
        })
        const searchText = this.data.searchText
        const newCompanyList = this.data.companyList.filter(item => {
            return item.name.indexOf(searchText) !== -1
        })
        const defaultCompanyList = newCompanyList.filter(item => !item.online)
        const onlineCpmpanyList = newCompanyList.filter(item => item.online && !item.status)
        const loadingCompanyList = newCompanyList.filter(item => item.online && item.status)
        this.setData({
            onlineCpmpanyList,
            defaultCompanyList,
            loadingCompanyList
        })
    },
    onLoad: async function () {
        await this.getCompanyList()
    },
    async onShow() {
        await this.getCompanyListInterval()
    },

    onUnload() {
        // 清楚计时器
        clearInterval(this.data.timer)
    },

    async getCompanyListInterval() {
        clearInterval(this.data.timer)
        this.data.timer = setInterval(async () => {
            await this.getCompanyList()
        }, 1000 * 60 * 2)
    },

    async getCompanyList() {
        const phoneNumber = store.getItem("phoneNumber")
        const res = await app.get(`${app.Api.getCompanyList}/${phoneNumber}`, {}, {
            loading: false
        })
        if (!res.rows.length) {
            this.setData({
                isNoData: true,
                companyList: []
            })
            store.setItem("hasMapping", false)
        } else {
            const defaultCompanyList = res.rows.filter(item => !item.online)
            const onlineCpmpanyList = res.rows.filter(item => item.online && !item.status)
            const loadingCompanyList = res.rows.filter(item => item.online && item.status)
            this.setData({
                isNoData: false,
                companyList: res.rows,
                onlineCpmpanyList,
                defaultCompanyList,
                loadingCompanyList
            })
            store.setItem('companyList', res.rows)
            store.setItem("hasMapping", true)
        }
    },
    handleClcik(e) {
        // 选择某一水泥厂之后，我们应该将用户选择的存储到本地，在其他页面用
        const selectCompany = e.currentTarget.dataset.info
        store.setItem('selectCompany', selectCompany)
        store.setItem('factoryId', selectCompany.id)
        store.clear("startTime")
        store.clear("endTime")
        wx.switchTab({
            url: '/pages/index/index'
        })
    }
});